/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function() {
    $(":input").bind("change", function(){
        this.removeClass("notvalid");
        this.addClass("errmsg");
    });
    
    $("#dateOfBirth").bind("click",function(){
        $("#dateOfBirth").datepicker({
            altFormat: "dd-mm-yy",
            changeYear: true,
            changeMonth: true,
            dateFormat: "dd-mm-yy",
            yearRange: "1921:2000"
        } );
    });

});

function validateData(){
    var text = $("#userName").val();
    var err = "";
    if (text.length < 6){
        $("#userName").addClass("notvalid");
        $("#errUserName").removeClass("errmsg");
        $("#errUserName").addClass("showErrMsg");
        err = "username is empty";
    }

    text = $("#password").val();
    if (text.length < 6){
        $("#password").addClass("notvalid");
        $("#errPassword").removeClass("errmsg");
        $("#errPassword").addClass("showErrMsg");
        err += "password is empty";
    }else{
        var retypePassword = $("#retypePassword").val();
        if (retypePassword != text){
            $("#password").addClass("notvalid");
            $("#retypePassword").addClass("notvalid");
            $("#errRetypePassword").removeClass("errmsg");
            $("#errRetypePassword").addClass("showErrMsg");
            err += "Password doesn't match";
        }
    }

    text = $("#email").val();
    if (!isEmail(text)){
        $("#email").addClass("notvalid");
        $("#errEmail").removeClass("errmsg");
        $("#errEmail").addClass("showErrMsg");
        err += "Email is not valid";
    }

    text = $("#fullName").val();
    if (text.length == 0){
        $("#fullName").addClass("notvalid");
        $("#errFullName").removeClass("errmsg");
        $("#errFullName").addClass("showErrMsg");
        err += "Fullname is empty";
    }

    text = $("#dateOfBirth").val();
    if (text.length == 0){
        $("#dateOfBirth").addClass("notvalid");
        $("#errDateOfBirth").removeClass("errmsg");
        $("#errDateOfBirth").addClass("showErrMsg");
        err += "Date of birth isn't selected";
    }

    text = $("#phoneNumber").val();
    if (text.length != 9 && text.length != 11){
        $("#phoneNumber").addClass("notvalid");
        $("#errPhoneNumber").removeClass("errmsg");
        $("#errPhoneNumber").addClass("showErrMsg");
        err += "Phone number isn't valid";
    }

    text = $("#address").val();
    if (text.length == 0 ){
        $("#address").addClass("notvalid");
        $("#errAddress").removeClass("errmsg");
        $("#errAddress").addClass("showErrMsg");
        err += "Address isn't valid";
    }

    if ($("#chkGhiNho").attr("checked") == false){
        $("#errCheckBox").removeClass("errmsg");
        $("#errCheckBox").addClass("showErrMsg");
        err += "You are not agree with term";
    }

    if (err == ""){
        return true;
    }
    alert(err);
    return false;
}

function isEmail(str){
    var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    return regex.test(str);
}